//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "math\fn_toShape.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

// Receives one of the following and convert it to a shape:
// a variable name will be extracted from the mission namespace and should match the following.
// a Marker  	=> Shape with the position, direction and size of the marker
// a Trigger	=> Shape with the position, direction and size of the trigger
// a Position	=> Shape with a 0 size and angle
// an Object 	=> Shape with a 0 size or object size and direction
// a Group		=> Shape with a 0 size and position and direction of the leader
private ["_shape","_input","_center","_size","_angle","_rectangle","_done","_area"];
DEFAULT_PARAM(_input,0,[]);
_shape = [];

_center = [0,0,0];
_angle = 0;
_size = [0,0];
_rectangle = false;

_done = false;
// Case where we receive a marker as the position
if (AND_ALSO(IS_STRING(_input),IS_MARKER(_input))) then {
	DEBUG_1("marker",_input);
	_angle = markerDir _input;
	_center = markerPos _input;
	_size = markerSize _input;
	_rectangle = ((markerShape _input) == "RECTANGLE");
	_done = true;
};
if (!_done && IS_STRING(_input)) then {
	if (!isnil {missionNameSpace getVariable _input}) then {
		_input = missionNameSpace getVariable _input;
		DEBUG_1("Mission Namespace",ARR_2(str(_input),_input));
	};
};
if (!_done) then {
	if (AND_ALSO(IS_OBJECT(_input),IS_TRIGGER(_input))) then {
		DEBUG_1("trigger",_input);
		_area = triggerArea _input;
		_size = [_area select 0, _area select 1];
		_angle = _area select 2;
		_rectangle = _area select 3;
		_center = getPos _input;
		_done = true;
	};
};
if (!_done) then {
	if (IS_OBJECT(_input)) then {
		DEBUG_1("object",typeof _input);
		_angle = getDir _input;
		_center = getPos _input;
		_size = sizeOf (typeOf _input);
		_size = [_size, _size];
		_done = true;
	};
};
if (!_done) then {
	if (IS_GROUP(_input)) then {
		DEBUG_1("group",_input);
		_angle = getDir (leader _input);
		_center = getPos (leader _input);
		_done = true;
	};
};
if (!_done) then {
	if (IS_ARRAY(_input)) then {
		DEBUG_1("array",_input);
		if (count _input > 0) then {
			if (IS_ARRAY((_input select 0))) then {
				_shape = (_input select 0);
				_done = true;
			} else {
				if (count _input == 3) then {
					_center = _input;
					_done = true;
				};
			};
		};
	};
};

if (_done) then {
	if (count _shape <= 0) then {
		_shape = [_center,_size,_angle,_rectangle];
	};
	_shape = [_shape] call FFUNC(getShape);
} else {
	_shape = [_center,_size,_angle,_rectangle];
};
TRACE_1("End",_shape);
TMFPROFILERSTOP;
_shape;